package org.example.domain.strategy.service.rule.factory;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.example.domain.strategy.model.entity.RuleActionEntity;
import org.example.domain.strategy.service.annotation.LogicStrategy;
import org.example.domain.strategy.service.rule.ILogicFilter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class DefaultLogicFactory {
    public Map<String, ILogicFilter<RuleActionEntity.RaffleBeforeEntity>> beforeLogicFilterMap = new ConcurrentHashMap<>();
    public Map<String, ILogicFilter<RuleActionEntity.RaffleCenterEntity>> centerLogicFilterMap = new ConcurrentHashMap<>();
    public Map<String, ILogicFilter<RuleActionEntity.RaffleAfterEntity>> afterLogicFilterMap = new ConcurrentHashMap<>();

    public DefaultLogicFactory(List<ILogicFilter<?>> logicFilters) {
        logicFilters.forEach(logicFilter -> {
            LogicStrategy logicStrategy = AnnotationUtils.findAnnotation(logicFilter.getClass(), LogicStrategy.class);
            if (null != logicStrategy) {
                switch (logicStrategy.logicMode().type) {
                    case "before":
                        beforeLogicFilterMap.put(logicStrategy.logicMode().getCode(), (ILogicFilter<RuleActionEntity.RaffleBeforeEntity>)logicFilter);
                        break;
                    case "center":
                        centerLogicFilterMap.put(logicStrategy.logicMode().getCode(), (ILogicFilter<RuleActionEntity.RaffleCenterEntity>)logicFilter);
                        break;
                    case "after":
                        afterLogicFilterMap.put(logicStrategy.logicMode().getCode(), (ILogicFilter<RuleActionEntity.RaffleAfterEntity>)logicFilter);
                }
            }
        });
    }


    @Getter
    @AllArgsConstructor
    public enum LogicModel {
        RULE_WIGHT("rule_weight","【抽奖前规则】根据抽奖权重返回可抽奖范围KEY", "before"),
        RULE_BLACKLIST("rule_blacklist","【抽奖前规则】黑名单规则过滤，命中黑名单则直接返回", "before"),
        RULE_LOCK("rule_lock", "【抽奖中规则】抽奖一定次数后，解锁奖品", "center")
        ;
        private final String code;
        private final String info;
        private final String type;
    }
}

